#include <stdint.h>
#include "uart.h"
#include "clock.h"



static __attribute__((naked)) void __Reset_Handler(void);
static __attribute__((naked)) void __NMI_Handler(void);
static __attribute__((naked)) void __HardFault_Handler(void);
static __attribute__((naked)) void __MemManage_Handler(void);
static __attribute__((naked)) void __BusFault_Handler(void);
static __attribute__((naked)) void __UsageFault_Handler(void);
static __attribute__((naked)) void __SVC_Handler(void);
static __attribute__((naked)) void __DebugMon_Handler(void);
static __attribute__((naked)) void __PendSV_Handler(void);
static __attribute__((naked)) void __SysTick_Handler(void);

typedef struct {
    uint32_t sp;
    void (*ira_handler[15])(void);
} s_vector_tab_t;

__attribute__((section(".init_vector"))) s_vector_tab_t s_vector_tab = {
    0x20001000,
    __Reset_Handler,  // 0x04 reset 
    __NMI_Handler,    // 0x08 NMI
    __HardFault_Handler, //0X0c hardfault
    __MemManage_Handler, //0x10 memManage
    __BusFault_Handler,  //0x14 busfault
    __UsageFault_Handler, //0x18 usagefault
    (void *)0,            //0X1C resv
    (void *)0,            //0X20 resv
    (void *)0,            //0X24 resv
    (void *)0,            //0X28 resv
    (void *)0,            //0X2c resv
    __SVC_Handler,        //0x30 svc
    __DebugMon_Handler,   //0x34 debugMonitor
    (void *)0,            //0X38 resv
    __SysTick_Handler,    //0x3c pendsv
};  

static void __mem_data_init(void)
{
    extern uint32_t flash_data_start;
    extern uint32_t ram_data_end;
    extern uint32_t ram_data_start;

    uint8_t *pflashStart = (uint8_t *)&flash_data_start;
    uint8_t *pramStart = (uint8_t *)&ram_data_start;
    uint8_t *pramEnd = (uint8_t *)&ram_data_end; 

    for (; pramStart < pramEnd; pramStart++) {
        *pramStart++ = *pflashStart++;
    }
}

static void __mem_bss_init(void)
{
    extern uint32_t ram_bss_data_start;
    extern uint32_t ram_bss_data_end;

    uint8_t *pstart = (uint8_t *)&ram_bss_data_start;
    uint8_t *pend = (uint8_t *)&ram_bss_data_end; 

    for (; pstart < pend; pstart++) {
        *pstart++ = 0;
    }
}

static __attribute__((naked)) void __Reset_Handler(void)
{
    int a = 10;
    int b = 12;

    extern int start_entry(void);

    system_clock_init();

    //mem init
    __mem_data_init();
    __mem_bss_init();

    //外设初始化
    systick_init();
    uart_init();

    start_entry();
}

static __attribute__((naked)) void __NMI_Handler(void)
{
    while (1) {

    }
}

static __attribute__((naked)) void __HardFault_Handler(void)
{
    while (1) {
        
    }    
}

static __attribute__((naked)) void __MemManage_Handler(void)
{
     while (1) {
        
    }   
}

static __attribute__((naked)) void __BusFault_Handler(void)
{
    while (1) {
        
    }
}

static __attribute__((naked)) void __UsageFault_Handler(void)
{
    while (1) {
        
    }
}

static __attribute__((naked)) void __SVC_Handler(void)
{
    while (1) {
        
    }
}

static __attribute__((naked)) void __DebugMon_Handler(void)
{
    while (1) {
        
    }    
}

static __attribute__((naked)) void __PendSV_Handler(void)
{
    while (1) {
        
    }
}

static __attribute__((naked)) void __SysTick_Handler(void)
{
    systick_cnt_inc();
}